package com.leetao.dingtalk.core.support;

import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.leetao.dingtalk.core.domain.MessageCorpconversation;
import com.leetao.dingtalk.core.domain.Token;
import com.leetao.dingtalk.core.domain.rest.Rest;
import org.springframework.util.NumberUtils;

import java.util.Optional;

/**
 * 钉钉结果-> rest 转换器
 *
 * @author 菅江晖
 * @date 2021/5/24 - 11:53
 */
public class DingTalkResponseToRestConverter {


    public static Rest<Token> convertToken(OapiGettokenResponse resp) {
        Long errcode = resp.getErrcode();
        String errmsg = resp.getErrmsg();
        if (!resp.isSuccess()) {
            return Rest.error(errcode.intValue(), errmsg);
        }

        String accessToken = resp.getAccessToken();
        Long expiresIn = resp.getExpiresIn();
        return Rest.success(errmsg, new Token(accessToken, expiresIn));
    }

    public static Rest<MessageCorpconversation> convertMessageCorpconversationRest(OapiMessageCorpconversationAsyncsendV2Response resp) {
        Long errcode = resp.getErrcode();
        String errmsg = resp.getErrmsg();
        if (!resp.isSuccess()) {
            return Rest.error(errcode.intValue(), errmsg);
        }

        Long taskId = resp.getTaskId();
        String requestId = resp.getRequestId();
        return Rest.success(errmsg, new MessageCorpconversation(taskId, requestId));
    }
}
